﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.Xml;
using ZilLion.Service.Core.CRMUnities.Config;
using ZilLion.Service.Core.Log;
using ZilLion.Service.Core.MongoDbManager.ObjectDb;
using ZL.Web.Utility.Config;
using ZLWeb.WebApiEntityBase;
using ZLWeb.WebApiEntityBase.Error;

namespace ZL.Web.Utility.Log
{
    
    public static class LogHelper
    {
        public static string LogConfigPath { get; set; }

        public static void LogException(Exception ex, string type,string dbname= "weberrorlog")
        {
            if (ZLConfig.ExceptionType == ExceptionLogType.MongoDB)
            {
                ErrorMessage msg = new ErrorMessage(ex, type);
                IObjectDbRepository DbRepository = new ObjectDbRepository(dbname, "ErrorMessage");
                DbRepository.Insert(msg);
            }
            if (ZLConfig.ExceptionType == ExceptionLogType.ExceptionLess)
            {
                ZilLionLogManager.Instance().Error(ex, new string[2] { (string)ConfigurationManager.AppSettings["Appid"], type });
                //IZilLionLogger ZilLionLogger=new ZilLionLoggerBase(LogConfigPath);
                ////ZilLionLogger.Error(ex);
                //ZilLionLogger.Error(ex, new string[2] { (string)ConfigurationManager.AppSettings["Appid"], type });
                ErrorMessage msg = new ErrorMessage(ex, type, (string)ConfigurationManager.AppSettings["Appid"]);
                IObjectDbRepository DbRepository = new ObjectDbRepository(dbname, "RecordErrorMessage");
                DbRepository.InsertWithexpire(msg, TimeSpan.FromDays(2));
            }
        }

        //public static void LogOpenCardException(CardInfo info, string dbname = "CRMEventDetail")
        //{
        //    IObjectDbRepository DbRepository = new ObjectDbRepository(dbname, "CardInfo");
        //    DbRepository.Insert(info);
        //}

        public static void LogRunningException(RunningMessage info, string dbname = "CRMEventDetail")
        {
            try
            {
                string RuningLog = ConfigurationManager.AppSettings["RuningLog"];
                if (RuningLog == "1")
                {
                    if (info.stream != null)
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(info.stream);
                        info.Message = info.Message + "xml:" + xmlDoc.OuterXml;
                    }
                    IObjectDbRepository DbRepository = new ObjectDbRepository(dbname, "LogRunning");
                    DbRepository.Insert(info);
                }
            }
            catch (Exception)
            {
            }
        }


        public static void LogRunningException1(RunningMessage info, string dbname = "CRMEventDetail")
        {
            try
            {
                IObjectDbRepository DbRepository = new ObjectDbRepository(dbname, "LogRunning");

                DbRepository.Insert(info);
            }
            catch (Exception exe)
            {
            }
        }
    }
}
